
using System.IO;
using System;
using System.Globalization;
using System.Text;
using System.Configuration;
using ODM2KMZ.Properties;

public partial class GetDataCSV : System.Web.UI.Page
{
 
    private void Page_Load(object sender, System.EventArgs e)
    {
   
    }
    private void Page_PreRender(object sender, System.EventArgs e)
    {
    
  
        Response.AppendHeader("Content-Disposition", "inline;filename=HISDataCSV.csv");
        Response.ContentType = "text/csv";
        Response.Write(GetCSVData()) ;
 }
    private void Page_Unload(object sender, System.EventArgs e)
    {
        
    }
   
    private string GetCSVData()
    {
        CultureInfo provider = new CultureInfo(Settings.Default.Culture);


        WaterOneFlow CurrentService = new WaterOneFlow();
        CurrentService.Url = Request.QueryString["URL"];
        TimeSeriesResponseType Data = CurrentService.GetValuesObject(
            Request.QueryString["SiteCode"],
            Request.QueryString["VariableCode"],
            DateTime.ParseExact(Request.QueryString["BeginDate"],"d",provider).ToString("s"),
            DateTime.ParseExact(Request.QueryString["EndDate"], "d", provider).ToString("s"),string.Empty);

        StringWriter  sw =  new StringWriter();
        StringBuilder sb = sw.GetStringBuilder();

        sb.AppendLine("HIS URL," + Request.QueryString["URL"]);
        sb.AppendLine("SiteCode," + Request.QueryString["SiteCode"]);
        sb.AppendLine("VariableCode," + Request.QueryString["VariableCode"] +
                      ",Units," + Data.timeSeries.values.unitsAbbreviation);
        sb.AppendLine("Count," + Data.timeSeries.values.count);
        sb.AppendLine("Download Date," + DateTime.Now.ToString("s"));
        sb.AppendLine("");
        sb.AppendLine("DateTime,Value,QACode");

         foreach ( ValueSingleVariable vsv in Data.timeSeries.values.value)
         {
             sb.AppendLine(vsv.dateTime.ToString("s")+"," +
                 vsv.Value + "," + vsv.qualityControlLevel);

         }
        return sw.ToString();
    }
}
